package org.com.android.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Good implements Itable {
	public final static String name = "Good";
	
	public final static String primarykey = "_id" ;
	
	public class Status {
		public final static int OnSale = 0;		//available
		public final static int Cached = 1;		//shipped but not submitted
		public final static int submitted = 2;	//shipped and submitted
	}

	private final Schema schema;

	public Good(Schema schema) {
		this.schema = schema;
	}
	
	public ArrayList<Map<String,String>> query(String[] columns,String selection, 
			String[] selectionArgs,String groupBy,String having,String orderBy,Integer limit){
		SQLiteDatabase db = schema.getdatabaseforread();
		Cursor cursor = db.query(Good.name, columns, selection, selectionArgs, groupBy, having, orderBy);
				
		ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>();

		if (cursor.moveToFirst()) {
			while (cursor.getPosition() != cursor.getCount()) {
				Map<String, String> item = new HashMap<String, String>();
				for (int columnIndex = 0 ; columnIndex < columns.length ; columnIndex ++) {	
					item.put(columns[columnIndex],cursor.getString(columnIndex));
					Log.d("Good::query",cursor.getString(columnIndex));
				}
				result.add(item);				
				cursor.moveToNext();
			}
		}

		cursor.close();
		db.close();
		Log.d("Good::query", "leave Good::query") ;
		return result;
	}

	public Cursor query(String[] columns, String selection, String[] selectionArgs,
			String groupBy, String having, String orderBy){
		SQLiteDatabase db = schema.getdatabaseforread();
		Cursor cursor = db.query(Good.name, columns, selection, selectionArgs, groupBy, having, orderBy);
		return cursor ;
	}

	public boolean insert(Map<String, String> item) {
		boolean result = false;
		if (item == null) {
			return result;
		}
		String sql = "INSERT INTO good(_id, status) VALUES (" + "\""
				+ item.get("_id") + "\"" + "," + "\"" + item.get("status")
				+ "\"" + ")";
		SQLiteDatabase db = schema.getdatabaseforwrite();
		db.beginTransaction();
		try {
			Log.d("Good::insert ", sql);
			db.execSQL(sql);
			db.setTransactionSuccessful();
			result = true;
		} catch (Exception e) {
			Log.e("Good::insert ", e.getMessage());
			result = false;
		} finally {
			db.endTransaction();
		}
		db.close();
		return result;
	}

	public boolean update(Map<String, String> item) {
		boolean result = false;
		if (item == null) {
			return result;
		}
		String sql = "update good set ";
		if (item.containsKey("status")) {
			sql += "status = " + item.get("status");
		}
		sql += " where _id = " + "\"" + item.get("_id") + "\"";
		SQLiteDatabase db = schema.getdatabaseforwrite();
		db.beginTransaction();
		try {
			db.execSQL(sql);
			Log.d("Good::update", sql);
			db.setTransactionSuccessful();
			return true;
		} catch (Exception e) {
			Log.e("Good::update", sql);
			return false;
		} finally {
			db.endTransaction();
			db.close();
		}
		
	}

	public boolean delete(Map<String, String> item) {
		boolean result = false;
		SQLiteDatabase db = schema.getdatabaseforwrite();
		String sql = "delete from goods where _id=";
		try {
			db.execSQL(sql, new Object[] { item.get("barcode") });
			Log.d("Good::delete", sql);
			result = true;
		} catch (Exception e) {
			Log.e("Good::delete", sql);
			result = false;
		}
		db.close();
		return result;
	}
}